/**
 * 
 */
package logic;

import java.math.BigInteger;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.sql.SQLException;

import javax.crypto.Cipher;



/**
 * @author Alejandro Casas
 *
 */
public class RSA {



	private static PrivateKey privateKey;
	static PublicKey publicKey;

	/**
	 * 
	 */
	public static void RSA() {



		//LEER del archivo properties
		//privateKey = 
		//publicKey = 

	}



	public static String encryptString(String str) throws Exception {
		//TODO
		/*
		RSA();
		try{




			Cipher aesCipher = Cipher.getInstance("RSA");  
			aesCipher.init(Cipher.ENCRYPT_MODE, publicKey);  

			byte[] ciphertext = aesCipher.doFinal(str.getBytes());
			return new String(ciphertext);
		}
		catch (Exception e) {
			throw new Exception("Encrypt Exception");
		}

		*/
		MessageDigest mdEnc;

		
		mdEnc = MessageDigest.getInstance("MD5");
		// Encryption algorithm
		mdEnc.update(str.getBytes(), 0, str.length());
		String strEncrypted = new BigInteger(1, mdEnc.digest()).toString(16); // Encrypted string
		
		return strEncrypted;

	}


	private static String decryptString(String str) throws Exception{
		RSA();
		try{



			Cipher aesCipher = Cipher.getInstance("RSA");  
			aesCipher.init(Cipher.DECRYPT_MODE, privateKey);  

			byte[] ciphertext = aesCipher.doFinal(str.getBytes());
			return new String(ciphertext);
		}
		catch (Exception e) {
			throw new Exception("Decrypt Exception");
		}
	}

	public static void main(String[] args) {
		KeyPairGenerator keyGen;
		try {
			keyGen = KeyPairGenerator.getInstance("RSA");

			KeyPair keyPair = keyGen.genKeyPair();
			privateKey = keyPair.getPrivate();
			publicKey = keyPair.getPublic();
			System.out.println(privateKey);
			System.out.println(publicKey);
		// guardar en properties
		

		} catch (NoSuchAlgorithmException e) {
			
			e.printStackTrace();
		}
	}


}
